package com.ruoyi.web.controller.index;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.controller.tool.DateTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.springframework.core.io.Resource;

@RestController
@RequestMapping("/system/index")
public class Index {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    private  String imagePath = "/Users/apple/IdeaProjects/demo/zichan/ruoyi-admin/src/main/resources/image/img.png"; // 图片路径
    private  String imagePath1 = "/Users/apple/IdeaProjects/demo/zichan/ruoyi-admin/src/main/resources/image/img_1.png"; // 图片路径
    @GetMapping(value = "/getImage")
    public ResponseEntity<Resource> getImagePath() throws IOException {
        String[] dateStrList = DateTimeUtils.getNowDateStr(DateTimeUtils.DATE_FORMAT_YYYY_MM_DD).split("-");
        int x = 100;
        int y = 142;
        BufferedImage originalImage = ImageIO.read(new File(imagePath));
        BufferedImage processedImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = processedImage.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        // 绘制原始图片
        g2d.drawImage(originalImage, 0, 0, null);
        // 设置绘图属性
        g2d.setFont(new Font("微软雅黑", Font.PLAIN, 13)); // 字体大小颜色等
        g2d.setColor(Color.darkGray); // 字体颜色
        // 设置字体粗细
        float strokeWidth = 0.0f; // 更细的字体，可以使用更细的线条
        BasicStroke stroke = new BasicStroke(strokeWidth);
        g2d.setStroke(stroke);
        int i=0;
        for (String time:dateStrList) {
            g2d.drawString(time, x, y);// 在图片上的坐标位置绘制文字
            if(i==1){
                x=x+60;
            }else {
                x=x+80;
            }
            i++;
        }
        x=630;
        y=580;
        int i1=0;
        for (String time:dateStrList) {
            g2d.drawString(time, x, y);// 在图片上的坐标位置绘制文字
            if(i1==1){
                x=x+50;
            }else {
                x=x+70;
            }
            i1++;
        }
        String[] dateList = DateTimeUtils.getNowDateStr(DateTimeUtils.DATETIME_FORMAT_HH_MM).split(":");
        x=800;
        y=580;
        for (String time:dateList) {
            g2d.drawString(time, x, y);// 在图片上的坐标位置绘制文字
            x=x+55;
        }
        String outputPath = "/Users/apple/IdeaProjects/demo/zichan/ruoyi-admin/src/main/resources/image2/"+ UUID.randomUUID() +".png"; // 输出图片路径
        ImageIO.write(processedImage, "png", new File(outputPath));// 输出图片
        g2d.dispose();
        // 加载图片资源
        Resource resource = loadResource(outputPath);
        // 构建响应
        return ResponseEntity.ok()
                .contentType(MediaType.IMAGE_PNG)
                .body(resource);
    }

    @GetMapping(value = "/getImage1")
    public ResponseEntity<Resource> getImagePath1() throws IOException {
        BufferedImage originalImage = ImageIO.read(new File(imagePath1));
        BufferedImage processedImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = processedImage.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        // 绘制原始图片
        g2d.drawImage(originalImage, 0, 0, null);
        // 设置绘图属性
        g2d.setFont(new Font("微软雅黑", Font.BOLD, 40)); // 字体大小颜色等
        g2d.setColor(Color.darkGray); // 字体颜色
        // 设置字体粗细
        float strokeWidth = 0.0f; // 更细的字体，可以使用更细的线条
        BasicStroke stroke = new BasicStroke(strokeWidth);
        g2d.setStroke(stroke);
        int x = 80;
        int y = 350;
        g2d.drawString("张*，您好!", x, y);// 在图片上的坐标位置绘制文字
        String outputPath = "/Users/apple/IdeaProjects/demo/zichan/ruoyi-admin/src/main/resources/image3/"+ UUID.randomUUID() +".png"; // 输出图片路径
        ImageIO.write(processedImage, "png", new File(outputPath));// 输出图片
        g2d.dispose();
        // 加载图片资源
        Resource resource = loadResource(outputPath);
        // 构建响应
        return ResponseEntity.ok()
                .contentType(MediaType.IMAGE_PNG)
                .body(resource);
    }


    @PostMapping(value = "/getWelcome")
    public AjaxResult getWelcome(){
        return AjaxResult.success("","张*，您好!");
    }

    @PostMapping(value = "/login")
    public AjaxResult login(@RequestBody LoginDTO loginDTO){
        String key = "LoginKey";
        String value = "";
        if(loginDTO.getUserName().equals("16789678654")&&loginDTO.getPassWord().equals("as123456")){
            value = UUID.randomUUID().toString();
            redisTemplate.opsForValue().set(key,value);
        }else {
            return AjaxResult.error("登陆失败");
        }
        return AjaxResult.success("",value);
    }





    private Resource loadResource(String imagePath) {
        // 实现加载图片资源的逻辑，这里可以是文件系统、类路径、网络等不同位置
        // 这里的示例是加载文件系统中的图片
        return new FileSystemResource(imagePath);
    }





    public static void main(String[] args) throws IOException {
        String imagePath = "/Users/apple/IdeaProjects/demo/zichan/ruoyi-admin/src/main/resources/image/img_1.png"; // 图片路径
        String outputPath = "/Users/apple/IdeaProjects/demo/zichan/ruoyi-admin/src/main/resources/image/output_1.png"; // 输出图片路径
        BufferedImage originalImage = ImageIO.read(new File(imagePath));
        BufferedImage processedImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = processedImage.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        // 绘制原始图片
        g2d.drawImage(originalImage, 0, 0, null);
        // 设置绘图属性
        g2d.setFont(new Font("微软雅黑", Font.BOLD, 40)); // 字体大小颜色等
        g2d.setColor(Color.darkGray); // 字体颜色
        // 设置字体粗细
        float strokeWidth = 0.0f; // 更细的字体，可以使用更细的线条
        BasicStroke stroke = new BasicStroke(strokeWidth);
        g2d.setStroke(stroke);
        int x = 80;
        int y = 350;
        g2d.drawString("张*，您好!", x, y);// 在图片上的坐标位置绘制文字
        ImageIO.write(processedImage, "png", new File(outputPath));// 输出图片
        g2d.dispose();

    }

}
